package com.gzstrong.springmvc.controller;


import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;



import com.alibaba.fastjson.JSONObject;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;



/***
 * 处理器的开发方式有多种，比如实现HttpRequestHandler接口、Controller接口的方式，还有注解的方式
 * 企业中使用的一般来讲都是注解的方式 注解的方式，主要有两个注意事项：
 * 类上加上@Controller注解（必须是Controller，可以通过源码找到答案） 类上或者方法上面要加上@RequestMapping注解（必须的）
 * 
 * @author think
 *
 */

@Controller
@RequestMapping("insertVideo")
public class ItemController {

	@PostMapping("insertValue")
	public Object insertValue(@RequestParam("loadway") String loadway,@RequestParam("startDate") String startDate,@RequestParam("endDate") String endDate,@RequestParam("szAccount") String szAccount,
							  @RequestParam("filename") String filename,@RequestParam("random") String random){
		try {
			//12345992222222
			Statement sql;
			ResultSet rs;
			String dbUrl = "jdbc:sqlserver://192.168.197.151:1433; DatabaseName=video";
			String userName = "sa"; //默认用户名
			String userPwd = "1510@Scproject"; //密码
			Connection dbConn;
			try {
				String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
				Class.forName(driverName);
				dbConn = DriverManager.getConnection(dbUrl, userName, userPwd);
				sql = dbConn.createStatement();
				String uuid = UUID.randomUUID().toString().replaceAll("-", "");

				//创建容器控制数据库
				try {

						sql.execute("insert into   videoMessage (id,loadway,startDate,endDate,szAccount,filename) VALUES ('" + uuid + "','" + loadway + "','" + startDate + "','" + endDate + "','" + szAccount + "','" + filename + "' )");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e.getMessage());
		}
		return  null;
	}

  //往安服系统插入数据
	@PostMapping("insertAnfuValue")
	public Object insertAnfuValue(@RequestParam("videourls") String videourls,@RequestParam("startDate") String startDate,@RequestParam("endDate") String endDate,@RequestParam("szAccount") String szAccount,
							  @RequestParam("id") String id,@RequestParam("random") String random){
		try {
			RestTemplate restTemplate=new RestTemplate();
			String url = "http://192.168.181.37:9010/eventanfulevel1";
			HttpHeaders httpHeaders = new HttpHeaders() ;
			//设置HTTP请求的请求头信息
			httpHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
			//设置相应内容，相应内容将被转换为json格式返回
			httpHeaders.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
			httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

			JSONObject post = new JSONObject();
			//	"buscode":szAccount,"flag":2,"endtime":endDate,"starttime":startDate,"id":id,"videourl":"192.168.197.180:8090/download2/66.mp4"
			post.put("buscode",szAccount);
			post.put("flag",2);
			post.put("endtime",endDate);
			post.put("videourl",videourls);
			post.put("startDate",startDate);
			post.put("id",id);
			System.out.println("11111111111111111:"+post);
			HttpEntity<String> httpEntity = new HttpEntity(post.toString(),httpHeaders) ;
			String	EE = restTemplate.postForObject(url, httpEntity, String.class,"application/json");

		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e.getMessage());
		}
		return  null;
	}

	@GetMapping("getAnfuValue")
	@ResponseBody
	public String getAnfuValue(){
		String val=null;
		try {
			RestTemplate restTemplate=new RestTemplate();
			String url = "http://192.168.181.37:9010/eventanfulevel1";
			 val = restTemplate.getForObject(url,String.class);
			System.out.print(val);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e.getMessage());
		}
		return  val;
	}
	// @RequestMapping此时填写的是url
	// ModelAndView:model表示的是数据模型，view就是最终要展示给用户的视图
	@GetMapping("test")
	@ResponseBody
	public String test(){
		return  "666";
	}

}
